package com.globalblue;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.globalblue.view.ImprovedMainWindow;

public class WindowManager {
	private ImprovedMainWindow window;
	public static final String BASE_FOLDER = "C:/Delivery";
	private static Logger logger = Logger.getLogger(WindowManager.class);

	public void loadWindow() {
		window = new ImprovedMainWindow();
		if(window.getWebsiteDirectory().getSelectedItem()==null){
			window.getWebsiteDirectory().addItem(Configurations.getWebsiteDirectory());
		window.getWebsiteDirectory().setSelectedItem(
				Configurations.getWebsiteDirectory());
		}
		window.showMainWindow(this);
	}

	public void performAction(final String ticketId,
			final String websiteFolder, final String buildPath,
			final String username, final String password) {
		try {
			logger.log(Level.INFO, "Writing Configuarations");
			ConfigManager manager = new ConfigManager();
			manager.writeConfig(websiteFolder);
			Configurations.setWebsiteDirectory(websiteFolder);
			Configurations.setBuildUrl(buildPath);
			Configurations.setTicketId(ticketId);
			Configurations.setUsername(username);
			logger.log(Level.INFO, "Finish Writing Configuarations");

			logger.log(Level.INFO, "Creating folder structure");
			FolderManager fm = new FolderManager();
			fm.createFolderStructure(BASE_FOLDER, ticketId);
			logger.log(Level.INFO, "Finish Creating folder structure");

			logger.log(Level.INFO, "Backuping existing web folder");
			window.changeStatus("images\\backup in progress.gif", "Backuping files...");
			BackupThreadHandler backupHandler = new BackupThreadHandler(
					ticketId, window, username, password);
			backupHandler.execute();

		} catch (Exception e) {
			logger.log(Level.ERROR, e);
		}
	}

}
